from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
driver = webdriver.Chrome()
driver.get('https://kyfw.12306.cn/otn/leftTicket/init')
driver.implicitly_wait(10)
#出发地
chufa=driver.find_element_by_css_selector('input[id="fromStationText"]')
chufa.click()
chufa.clear()
chufa.send_keys('深圳\n')
time.sleep(2)

#目的的
destination = driver.find_element_by_css_selector('input[id="toStationText"]')
destination.click()
destination.click()
destination.send_keys('武汉\n')

# #用Select方法找发车时间06-12
# Select(driver.find_element_by_id('cc_start_time')).select_by_visible_text('06:00--12:00')

#css找发车时间为06-12
driver.find_element_by_css_selector('#cc_start_time :nth-child(3)').click()

#选择第二天（10月11日）
driver.find_element_by_css_selector('#date_range >ul>li:nth-child(2)').click()


# print('\n\n\n===============================\n\n\n')
# xpath ='//*[@id="queryLeftTable"]//td[4][@class]/../td[1]//a'
#
# theTrains = driver.find_elements_by_xpath(xpath)
# for one in theTrains:tr
#     print (one.text)

#css语法
print('\n\n\n===============================\n\n\n')
ele= driver.find_elements_by_css_selector(' #queryLeftTable tr ')
# 先不加这个，发现特别慢
driver.implicitly_wait(0)
for one in ele:
    secondlevelseat = one.find_elements_by_css_selector('td:nth-of-type(4)[class]')
    if secondlevelseat:
        print (one.find_element_by_css_selector('td:nth-of-type(1) a').text)

driver.quit()